<?php
/* 
 * to use database
 * i create this plugin
 */
class Db_Plugin extends Plugin{
    private $db_type='mysql';
    private $last_insert_id=null;
    private $num_rows=null;
    private $affected_rows=null;
    private $dblink=null;
    private $db_user='root';
    private $db_pwd='123456';
    private $host='127.0.0.1';
    private $dbname='mydb';

    private function get_connection(){
        switch($this->db_type){
            case 'mysql':
                if (empty ($this->dblink)) {
                    $this->dblink=mysql_connect($this->host,$this->db_user, $this->db_pwd);
                    if (!$this->dblink) {
                        #
                        return false;
                    }
                    mysql_select_db($this->dbname,$this->dblink);
                }
                break;
            
        }
        return true;
    }
    function query($sql){
        if (!$this->get_connection()) {
            return false;
        }
        $this->num_rows=null;
        $this->last_insert_id=null;
        $this->affected_rows=null;
        switch ($this->db_type){
            case 'mysql':
                if($result=mysql_query($sql, $this->dblink)){
                    if ($result===true) {
                        $this->last_insert_id=mysql_insert_id($this->dblink);
                        $this->affected_rows=mysql_affected_rows($this->dblink);
                        return true;
                    }else{
                        $this->num_rows=mysql_num_rows($result);
                        if ($this->num_rows==0) {
                            return array();
                        }
                        $r=array();
                        while($line = mysql_fetch_assoc($result)){
                            $r[]=$line;
                        }
                        return $r;
                    }
                }else{
                    return false;
                }
                break;
            
        }
    }
    function num_rows(){
        return $this->num_rows;
    }
    function last_insert_id(){
        return $this->last_insert_id;
    }
    function affected_rows(){
        return $this->affected_rows;
    }
}
?>